package com.write.program.socket;

import com.write.program.socket.message.LoginMessage;
import com.write.program.socket.message.Message;

import java.io.IOException;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * Description: 客户端发起的socket的管理
 * User: Endstart
 * Date: 2019-11-20
 * Time: 23:42
 */
public class ClientManager {
    private ClientSocketService clientSocketService;

    public ClientManager(String serverIp, int serverPort) throws IOException {
        clientSocketService = new ClientSocketService(new Socket(serverIp, serverPort));
        clientSocketService.start();
    }

    public void sendMessageToServer(Message message) throws IOException {
        if (!clientSocketService.isAlive()) {
            System.out.println("please login");
            return;
        }
        clientSocketService.sendMessage(message);
    }

    public void startLogin(String username,String pwd) throws IOException {
        LoginMessage loginMessage = new LoginMessage(username, pwd);
        sendMessageToServer(loginMessage);
    }


}
